﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AnalyticsGadget.Models
{
    public class ReportQuery
    {
        private List<DimensionType> _dimensionList;
        private List<SortBy> _sortyByList;

        public ReportQuery(string profileID, MetricType metric)
        {
            ProfileID = profileID;
            Metric = metric;

            _dimensionList = new List<DimensionType>(); 
            _sortyByList = new List<SortBy>();
        }

        public DateTime? StartDate { get; set; }
        public DateTime? StopDate { get; set; }
        public string ProfileID { get; private set; }
        public MetricType Metric { get; private set; }
        public Timespan Timespan { get; private set; }

        public DimensionType[] Dimensions { get { return _dimensionList.ToArray(); } }
        public SortBy[] SortyByList { get { return _sortyByList.ToArray(); } }
        public int MaxRecords { get; set; }

        public void AddSortBy(DimensionType dimension, SortDirection direction)
        {
            AddSortBy(dimension.ToString().ToLower(), direction);
        }
        public void AddSortBy(MetricType metric, SortDirection direction)
        {
            AddSortBy(metric.ToString().ToLower(), direction);
        }
        private void AddSortBy(string dataField, SortDirection direction)
        {
            _sortyByList.Add(new SortBy() { DataField = dataField, Direction = direction });
        }
        public void AddDimension(DimensionType dimension)
        {
            _dimensionList.Add(dimension);
        }
        public void SetTimespan(Timespan timespan)
        {
            Timespan = timespan;

            StopDate = DateTime.Now;

            switch (timespan)
            { 
                case Timespan.Year:
                    StartDate = StopDate.Value.AddYears(-1);
                    break;
                case Timespan.Month:
                    StartDate = StopDate.Value.AddMonths(-1);
                    break;
                case Timespan.Week:
                    StartDate = StopDate.Value.AddDays(-7);
                    break;
                case Timespan.Day:
                    StartDate = StopDate.Value.AddDays(-1);
                    break;

            }
        }
    }
   
}
